package com.yyx.beautifylib.ui.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.muzhi.camerasdk.library.filter.GPUImage;
import com.muzhi.camerasdk.library.filter.GPUImageView;
import com.muzhi.camerasdk.library.utils.PhotoEnhance;
import com.yyx.beautifylib.R;
import com.yyx.beautifylib.model.BLEnhanceParam;
import com.yyx.beautifylib.model.BLReliefParam;
import com.yyx.beautifylib.model.BLReliefParam;
import com.yyx.beautifylib.scrawl.DrawingBoardView;
import com.yyx.beautifylib.scrawl.ScrawlTools;
import com.yyx.beautifylib.ui.activity.base.BLToolBarActivity;
import com.yyx.beautifylib.utils.MyImageProcessor;

import java.util.Stack;

import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodlePath;
import cn.hzw.doodle.DoodlePen;
import cn.hzw.doodle.DoodleShape;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleColor;
import cn.hzw.doodle.core.IDoodleItemListener;
import cn.hzw.doodle.core.IDoodlePen;
import cn.hzw.doodle.core.IDoodleSelectableItem;
import cn.hzw.doodle.core.IDoodleTouchDetector;
/**
 * @Desc:浮雕
 * @Author:  86133
 * @Time:  2020/4/11
 */
public class BLReliefActivity extends BLToolBarActivity implements View.OnClickListener {
    private GPUImageView mIvSource;

    private BLReliefParam mParam;
    private Bitmap mSource;

    private FrameLayout mFrameLayout;
    private ImageView relief_yes;
    private ImageView return_last;

    private Stack<Bitmap> pictureForRevoke;
    private int maxRevokePictures;

    private boolean canChange = true;

    @Override
    protected int getContentLayoutId() {
        return R.layout.bl_activity_relief;
    }

    @Override
    protected void customToolBarStyle() {
        mToolbar.setTitle("浮雕");
        mToolbar.inflateMenu(R.menu.menu_preview);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.preview_menu) {
                    setResult(RESULT_OK);
                    BLReliefParam.recycleBitmap();
                    BLReliefParam.bitmap = mSource;
                    onBackPressed();
                }
                return false;
            }
        });
    }

    @Override
    protected void initView() {
        mIvSource = getViewById(R.id.enhance_image);

        mFrameLayout = getViewById(R.id.doodle_container);
        relief_yes = getViewById(R.id.relief_yes);
        return_last = getViewById(R.id.return_last);
    }

    @Override
    protected void otherLogic() {
        initRevokeValue();
        mParam = getIntent().getParcelableExtra(BLReliefParam.KEY);
        mSource = BLReliefParam.bitmap;

        mIvSource.setScaleType(GPUImage.ScaleType.CENTER_INSIDE);
        mIvSource.setImage(mSource);
    }

    private void initRevokeValue(){
        pictureForRevoke=new Stack<>();
        maxRevokePictures=9;
    }

    private void putPictureIntoRevokeStack(){
        if(pictureForRevoke.size()>=maxRevokePictures){
            pictureForRevoke.remove(0);
        }
        Bitmap currentBitMap = mIvSource.getCurrentBitMap();
        pictureForRevoke.push(currentBitMap);
    }

    @Override
    protected void setListener() {
        relief_yes.setOnClickListener(this);
        return_last.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int resId = v.getId();
        if (resId == R.id.relief_yes) {
            if(canChange){
                putPictureIntoRevokeStack();
                reliefPicture();
                relief_yes.setImageResource(R.drawable.mosaic_three_un);
            }
            canChange = false;
        } else if (resId == R.id.return_last) {
            relief_yes.setImageResource(R.drawable.mosaic_three);
            revokePicture();
            canChange = true;
        }
    }

    private void reliefPicture(){
        Bitmap reliefBitmap= MyImageProcessor.reliefPicture(mSource);
        mSource = reliefBitmap;
        mIvSource.setImage(mSource);
    }

    private void revokePicture(){
        if(!pictureForRevoke.empty()){
            mIvSource.setImage(pictureForRevoke.peek());
            pictureForRevoke.pop();
        }
    }
}
